package ar.com.gep.soccer.utils.engine;

import java.util.List;
import java.util.Map;
import java.util.Random;

import ar.com.gep.soccer.services.dto.GameDTO;

/**
 * Singleton
 * 
 * @author guillermo
 */
public class Engine {

	private static Engine INSTANCE = new Engine();

	private Map<Integer, List<Integer>> randoms;

	private Engine() {
		randoms = EngineValues.getValues();
	}

	public static final Engine getEngine() {
		return INSTANCE;
	}

	/**
	 * 
	 * @param homeSkills
	 * @return
	 */
	private final Integer calculeHome(List<Integer> homeSkills) {
		Random rand = new Random();
		Integer skill = rand.nextInt(10);
		Integer percent = rand.nextInt(101);
		List<Integer> percents = randoms.get(homeSkills.get(skill.intValue()));
		Integer goal = 0;
		for (Integer value : percents) {
			if (value >= percent) {
				return goal;
			}
			goal++;
		}
		return goal;
	}

	/**
	 * 
	 * @param game
	 * @param homeSkills
	 * @param awaySkills
	 */
	public final void playGame(GameDTO game, List<Integer> homeSkills, List<Integer> awaySkills) {
		game.setGoalHome(calculeHome(homeSkills));
		game.setGoalAway(calculeHome(awaySkills));
	}

}